package cn.platform.service;

import cn.platform.entity.Comment;
import cn.platform.mapper.CommentMapper;
import cn.platform.request.CommentRequest;
import cn.platform.request.CommentViewRequest;
import cn.platform.response.AResponse;
import cn.platform.response.CommentViewResponse;
import cn.platform.response.PageResult;
import cn.platform.util.ErrorCode;
import cn.platform.util.identify.SessionToken;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CommentService {

    @Resource
    CommentMapper commentMapper;

    /**
     * 返回前段展示数据
     */
    public AResponse listCommentView(CommentViewRequest req) {
        if(req.getPageNum() != null && req.getPageSize() != null){
            PageHelper.startPage(req.getPageNum(), req.getPageSize());
        }
        List<CommentViewResponse> list = commentMapper.listCommentView(req);
        if(req.getSenderType() == null && !req.getSender().equals("")){
            return AResponse.success(new PageInfo<CommentViewResponse>(list)).aSetMessage("未选择身份时，发送人搜索项无效哦");
        }
        return AResponse.success(new PageInfo<CommentViewResponse>(list)).aSetMessage("成功");
    }

    /**
     * 暂时保留
     */
    public AResponse listComment(CommentRequest req){
        if(req.getPageNum() != null && req.getPageSize() != null){
            PageHelper.startPage(req.getPageNum(), req.getPageSize());
        }
        List<Comment> list = commentMapper.listComment(req.getComment());
        return AResponse.success(new PageInfo<Comment>(list));
    }

    /**
     * 发送评论
     */
    public AResponse insertComment(Comment req){
        String role = SessionToken.getUserAccount().getRole();
        switch (role){
            case "teacher", "parent", "student":
                break;
            default:
              return AResponse.error(ErrorCode.UNAUTHORIZED, "该角色未授权");
        }
        req.setSenderType(role);
        req.setSenderID(SessionToken.getUserAccount().getId());
        req.setSendTime(LocalDateTime.now());
        commentMapper.insertComment(req);
        log.info("发送评论，角色：{}, 用户名：{}", SessionToken.getUserAccount().getRole(), SessionToken.getUserAccount().getUsername());
        return AResponse.success().aSetMessage("评论成功");
    }

    /**
     * 暂时保留
     */
    public AResponse updateComment(Comment req){
        commentMapper.updateComment(req);
        return AResponse.success().aSetMessage("更新成功");
    }

    /**
     * 删除评论
     */
    public AResponse deleteComment(int id){
        Comment com = new Comment();
        com.setId(id);
        com = commentMapper.listComment(com).getFirst();
        String targetRole = com.getSenderTypeEn();
        Integer targetID = com.getSenderID();
        
        boolean flag = false;
        String currentRole = SessionToken.getUserAccount().getRole();
        Integer currentID = SessionToken.getUserAccount().getId();
        // teacher和admin有权删除全部评论
        switch (currentRole){
            case "teacher", "admin":
                flag = true;
                break;
        }
        // parent和student只能删除自己发的
        switch (currentRole){
            case "student", "parent":
                if(targetRole.equals(currentRole) && currentID.equals(targetID)){
                    flag = true;
                }
                break;
        }
        if(flag){
            commentMapper.deleteComment(id);
            log.info("删除评论 -> id:{}，角色：{}, 用户名：{}",id, SessionToken.getUserAccount().getRole(), SessionToken.getUserAccount().getUsername());
            return AResponse.success().aSetMessage("删除成功");
        }
        return AResponse.error(ErrorCode.UNAUTHORIZED, "行为未授权");
    }
}
